package com.example.com.skills_utf8;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.ArrayMap;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;


// http://blog.csdn.net/hjj0212/article/details/8824674
public class ImageTextViewActivity extends Activity{
	
	
	ImageTextViewActivity activity = this;
	
	
	TextView mSubjectDetailView;
	EditText et;




	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_image_textview);
		
		mSubjectDetailView = (TextView) findViewById(R.id.subject_detail);
        et = (EditText) findViewById(R.id.et);
		
        CharSequence text = "如图所示★，dsfdsfdddd，如果fdsfs★东东";
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        String rexgString = "★";
        Pattern pattern = Pattern.compile(rexgString);
        Matcher matcher = pattern.matcher(text);

        // 讲上图中标示部分替换成图标
        while (matcher.find()) {
            builder.setSpan(
                    new ImageSpan(this, R.drawable.ic_launcher), matcher.start(), matcher
                            .end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        mSubjectDetailView.setText(builder);
        
        
        
        Drawable drawable = getResources().getDrawable(R.drawable.f001); 
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
        //需要处理的文本，[smile]是需要被替代的文本 
        SpannableString spannable = new SpannableString("[smile]"); 
        //要让图片替代指定的文字就要用ImageSpan 
        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
        spannable.setSpan(span, et.getText().length(),et.getText().length()+"[smile]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);   
        et.setText(spannable); 
        
        
        
		
        
        
        
	}


	

}



